using AutoMapper;
using DealerPlatform.Common.EventBusHelper;
using DealerPlatform.Core.Repository;
using DealerPlatform.Domain;
using DealerPlatform.Service.CustomerApp.Dtos;
using DealerPlatform.Service.OrderApp.Dtos;

namespace DealerPlatform.Service.CustomerApp;

public partial class CustomerService : ICustomerService
{
    private readonly IRepository<Customer> customerRepository;
    private readonly IRepository<CustomerInvoice> customerInvoiceRepository;
    private readonly IRepository<CustomerPwd> customerPwdRepository;
    private readonly IMapper mapper;

    public CustomerService(
        IRepository<Customer> customerRepository,
        IRepository<CustomerInvoice> customerInvoiceRepository,
        IRepository<CustomerPwd> customerPwdRepository,
        IMapper mapper,
        LocalEventBus<SaleOrderDto> saleOrderDtoLocalEventBus)
    {
        this.customerRepository = customerRepository;
        this.customerInvoiceRepository = customerInvoiceRepository;
        this.customerPwdRepository = customerPwdRepository;
        this.mapper = mapper;
        saleOrderDtoLocalEventBus.localEventHandler += SaleOrderDtoLocalEventHandler;
    }

    public async Task<Customer> GetCustomerAsync(string customerNo)
    {
        return await customerRepository.GetAsync(m => m.CustomerNo == customerNo);
    }

    public async Task<CustomerDto> GetCustomerDto(string customerNo)
    {
        var customer = await customerRepository.GetAsync(m => m.CustomerNo == customerNo);
        return mapper.Map<Customer, CustomerDto>(customer);
    }
}